package network.websocket;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.log4j.Logger;
import util.JsonHelper;


/**
 * 完成对ws通信对象的构造和解析
 *
 * @author wyl
 */
public class WSDtoConverter {
    private static final Logger logger = Logger.getLogger(WSDtoConverter.class);

    //将对象序列化包装到WSDto中,返回序列化后WSDto的序列化值
    public static String buildWSDtoJson(Object o) {
        try {
            WSDto dto = new WSDto();
            dto.typeName = o.getClass().getSimpleName();
            dto.dataJson = JsonHelper.toJson(o);
            return JsonHelper.toJson(dto);
        } catch (JsonProcessingException e) {
            logger.fatal("WSDto序列化失败", e);
            e.printStackTrace();
        }
        return null;
    }

    //将json串转换为WSDto
    public static WSDto readJsonToWSDto(String json) {
        try {
            return JsonHelper.jsonToObject(json, WSDto.class);
        } catch (JsonProcessingException e) {
            logger.fatal("WSDto反序列化失败", e);
            e.printStackTrace();
        }
        return null;
    }

}
